﻿using System;
using System.Linq;

public partial class administrator_edit_news : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadNhomTin();
            LoadData();
        }
    }
    protected void LoadNhomTin()
    {
        drdlnhomtin.DataSource = from o in db.Cates where o.TypeCateID == 1 && o.Parent == 0 select o;
        drdlnhomtin.DataTextField = "Name";
        drdlnhomtin.DataValueField = "Cate_ID";
        drdlnhomtin.DataBind();
    }
    protected void UpdateNews(object s,EventArgs e)
    {
        //if(chkchonlai.Checked)
        //{
        //    if (GetFilePic()=="")
        //    {
                var news =
                    (from o in db.News where o.NewsID == Convert.ToDecimal(Request.Cookies["ckNewsID"].Value) select o).SingleOrDefault();
        if (news != null)
        {
            news.Cate_ID = chkchonlai.Checked ? Convert.ToInt32(drdlnhomtin.SelectedValue) : news.Cate_ID;
            news.Image = GetFilePic()=="" ? news.Image : GetFilePic();
            news.Title = txttieude.Text;
            news.Quote = txttomtat.Text;
            news.Contents = FCK.Text;
            news.UpdateBy = "admin";
            db.SubmitChanges();
            lblthongbao.Text = "cập nhật thành công";
        }
    }
    public string GetFilePic()
    {
        var sfile = "";
        if (FileUpload1.HasFile)
        {
            sfile = Guid.NewGuid().ToString().Substring(0, 10) + FileUpload1.PostedFile.FileName.Remove(0, FileUpload1.PostedFile.FileName.LastIndexOf("."));
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + sfile);
        }
        return sfile;
    }
    protected void LoadData()
    {
        var news =
            (from o in db.News where o.NewsID == Convert.ToDecimal(Request.Cookies["ckNewsID"].Value) select o).
                SingleOrDefault();
        txttieude.Text = news.Title;
        txttomtat.Text = news.Quote;
        FCK.Text = news.Contents;

    }
}